﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Model;
using System.Xml.Linq;

namespace Maticsoft.Common
{
    public class XmlHelper
    {
        /// <summary>
        /// 获取集合
        /// </summary>
        /// <returns></returns>
        public static List<Title> getTitle()
        {
            List<Title> listTitle = new List<Title>();
            IEnumerable<XElement> elLesson = getLessonXML();

            foreach (var les in elLesson)
            {
                Title title = new Title();

                title.TitleID = les.Attribute("id").Value;
                title.TitleName = les.Attribute("name").Value;
                title.TitleUrl = les.Attribute("url").Value;
                title.TitleTypeIds = les.Attribute("typeids").Value;

                listTitle.Add(title);
            }
            return listTitle;
        }

        public static IEnumerable<XElement> getLessonXML()
        {
            XElement element = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + "/Admin/Title/Title.xml");
            IEnumerable<XElement> elPart = element.Elements("title");
            return elPart;
        }

        public static void AddXML(string titleID, string titleName, string titleUrl, string titleTypeIds, string path)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(path);
            XmlElement xmlelem;
            XmlNodeList xmlnodelist = xmldoc.ChildNodes;
            foreach (XmlNode xmlenode in xmlnodelist)
            {
                if (xmlenode.Name.ToString() == "root")
                {
                    xmlelem = (XmlElement)xmlenode;

                    XmlElement xmlelemtitle = xmldoc.CreateElement("title");//创建信息节点

                    XmlAttribute xmlattrID = xmldoc.CreateAttribute("id");
                    xmlattrID.Value = titleID;
                    xmlelemtitle.Attributes.Append(xmlattrID);

                    XmlAttribute xmlattrName = xmldoc.CreateAttribute("name");
                    xmlattrName.Value = titleName;
                    xmlelemtitle.Attributes.Append(xmlattrName);

                    XmlAttribute xmlattrUrl = xmldoc.CreateAttribute("url");
                    xmlattrUrl.Value = titleUrl;
                    xmlelemtitle.Attributes.Append(xmlattrUrl);

                    XmlAttribute xmlattrTypeIds = xmldoc.CreateAttribute("typeids");
                    xmlattrTypeIds.Value = titleTypeIds;
                    xmlelemtitle.Attributes.Append(xmlattrTypeIds);

                    xmlelem.AppendChild(xmlelemtitle);
                    xmldoc.AppendChild(xmlelem);
                    xmldoc.Save(path);
                }
            }
        }

        public static void UpdateTitle(string id, string name, string url, string ids, string path)
        {
            if (id != "")
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(path);

                XmlNode xn = xmldoc.SelectSingleNode(string.Format("root/title[@id={0}]", id));

                if (xn != null)
                {
                    xn.Attributes[3].InnerText = ids;
                }
                xmldoc.Save(path);
            }
        }

        public static void DelteTitle(string id, string path)
        {
            if (id != "")
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(path);
                XmlNode xn1 = xmldoc.SelectSingleNode("root");
                XmlNode xn = xmldoc.SelectSingleNode(string.Format("root/title[@id={0}]", id));

                xn1.RemoveChild(xn);
                xmldoc.Save(path);
            }
        }
    }
}
